<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class T_pegawai extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();

		$this->load->library( 'template' ); 
		$this->load->model( 'model_t_pegawai' ); 
		
		$this->load->helper( 'form' );
		$this->load->helper( 'language' ); 
		$this->load->helper( 'url' );
        $this->load->model( 'model_auth' );

        $this->logged_in = $this->model_auth->check( TRUE );
        $this->template->assign( 'logged_in', $this->logged_in );

		$this->lang->load( 'db_fields', 'english' ); // This is the language file
	}



    /**
     *  LISTS MODEL DATA INTO A TABLE
     */         
    function index( $page = 0 )
    {
        $this->model_t_pegawai->pagination( TRUE );
		$data_info = $this->model_t_pegawai->lister( $page );
        $fields = $this->model_t_pegawai->fields( TRUE );
        

        $this->template->assign( 'pager', $this->model_t_pegawai->pager );
		$this->template->assign( 't_pegawai_fields', $fields );
		$this->template->assign( 't_pegawai_data', $data_info );
        $this->template->assign( 'table_name', 'T_pegawai' );
        $this->template->assign( 'template', 'list_t_pegawai' );
        
		$this->template->display( 'frame_admin.tpl' );
    }



    /**
     *  SHOWS A RECORD VIEW
     */
    function show( $id )
    {
		$data = $this->model_t_pegawai->get( $id );
        $fields = $this->model_t_pegawai->fields( TRUE );
        

        
        $this->template->assign( 'id', $id );
		$this->template->assign( 't_pegawai_fields', $fields );
		$this->template->assign( 't_pegawai_data', $data );
		$this->template->assign( 'table_name', 'T_pegawai' );
		$this->template->assign( 'template', 'show_t_pegawai' );
		$this->template->display( 'frame_admin.tpl' );
    }



    /**
     *  SHOWS A FROM, AND HANDLES SAVING IT
     */         
    function create( $id = false )
    {
		$this->load->library('form_validation');
        
		switch ( $_SERVER ['REQUEST_METHOD'] )
        {
            case 'GET':
                $fields = $this->model_t_pegawai->fields();
                
                
                
                $this->template->assign( 'action_mode', 'create' );
        		$this->template->assign( 't_pegawai_fields', $fields );
                $this->template->assign( 'metadata', $this->model_t_pegawai->metadata() );
        		$this->template->assign( 'table_name', 'T_pegawai' );
        		$this->template->assign( 'template', 'form_t_pegawai' );
        		$this->template->display( 'frame_admin.tpl' );
            break;

            /**
             *  Insert data TO t_pegawai table
             */
            case 'POST':
                $fields = $this->model_t_pegawai->fields();

                /* we set the rules */
                /* don't forget to edit these */
				$this->form_validation->set_rules( 'NOMOR_INDUK', lang('NOMOR_INDUK'), 'required|max_length[9]' );
				$this->form_validation->set_rules( 'NM_PEGAWAI', lang('NM_PEGAWAI'), 'required|max_length[50]' );
				$this->form_validation->set_rules( 'INISIAL', lang('INISIAL'), 'required|max_length[3]' );
				$this->form_validation->set_rules( 'KOTA_LAHIR', lang('KOTA_LAHIR'), 'required|max_length[50]' );
				$this->form_validation->set_rules( 'TANGGAL_LAHIR', lang('TANGGAL_LAHIR'), 'required' );
				$this->form_validation->set_rules( 'KD_JENIS_KELAMIN', lang('KD_JENIS_KELAMIN'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'KD_GOL_DARAH', lang('KD_GOL_DARAH'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'KD_AGAMA', lang('KD_AGAMA'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'KD_STATUS_NIKAH', lang('KD_STATUS_NIKAH'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'ALAMAT', lang('ALAMAT'), 'required|max_length[150]' );
				$this->form_validation->set_rules( 'KD_POS', lang('KD_POS'), '5' );
				$this->form_validation->set_rules( 'NO_TELP', lang('NO_TELP'), '30' );
				$this->form_validation->set_rules( 'KD_JENIS_KETENAGAAN', lang('KD_JENIS_KETENAGAAN'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'NO_HP', lang('NO_HP'), '30' );
				$this->form_validation->set_rules( 'DIR_FOTO', lang('DIR_FOTO'), '255' );
				$this->form_validation->set_rules( 'USERNAME', lang('USERNAME'), 'required|max_length[20]' );
				$this->form_validation->set_rules( 'TANGGAL_AKSES', lang('TANGGAL_AKSES'), 'required' );
				$this->form_validation->set_rules( 'NOMOR_INDUK_BARU', lang('NOMOR_INDUK_BARU'), '18' );
				$this->form_validation->set_rules( 'NUPTK', lang('NUPTK'), '16' );
				$this->form_validation->set_rules( 'GELAR_DEPAN', lang('GELAR_DEPAN'), '20' );
				$this->form_validation->set_rules( 'GELAR_BELAKANG', lang('GELAR_BELAKANG'), '20' );
				$this->form_validation->set_rules( 'NIY_NIGK', lang('NIY_NIGK'), '20' );
				$this->form_validation->set_rules( 'NIK', lang('NIK'), '20' );
				$this->form_validation->set_rules( 'NM_IBU_KANDUNG', lang('NM_IBU_KANDUNG'), '100' );
				$this->form_validation->set_rules( 'RT', lang('RT'), '3' );
				$this->form_validation->set_rules( 'RW', lang('RW'), '3' );
				$this->form_validation->set_rules( 'KELURAHAN_DESA', lang('KELURAHAN_DESA'), '100' );
				$this->form_validation->set_rules( 'KECAMATAN', lang('KECAMATAN'), '100' );
				$this->form_validation->set_rules( 'KABUPATEN_KOTA', lang('KABUPATEN_KOTA'), '100' );
				$this->form_validation->set_rules( 'PROPINSI', lang('PROPINSI'), '100' );
				$this->form_validation->set_rules( 'KD_AREA', lang('KD_AREA'), '5' );
				$this->form_validation->set_rules( 'EMAIL', lang('EMAIL'), '255' );
				$this->form_validation->set_rules( 'STATUS_KEPEGAWAIAN', lang('STATUS_KEPEGAWAIAN'), '1' );
				$this->form_validation->set_rules( 'SERTIFIKASI_JABATAN', lang('SERTIFIKASI_JABATAN'), '1' );
				$this->form_validation->set_rules( 'TAHUN_SERTIFIKAT_JABATAN', lang('TAHUN_SERTIFIKAT_JABATAN'), '4' );
				$this->form_validation->set_rules( 'NOMOR_SERTIFIKAT_JABATAN', lang('NOMOR_SERTIFIKAT_JABATAN'), '50' );
				$this->form_validation->set_rules( 'KD_SERTIFIKASI_BIDANG_STUDI', lang('KD_SERTIFIKASI_BIDANG_STUDI'), '3' );
				$this->form_validation->set_rules( 'KD_PROGRAM_KEAHLIAN', lang('KD_PROGRAM_KEAHLIAN'), '2' );
				$this->form_validation->set_rules( 'LISENSI_KEPALA_SEKOLAH', lang('LISENSI_KEPALA_SEKOLAH'), '1' );
				$this->form_validation->set_rules( 'CATATAN', lang('CATATAN'), '250' );

				$data_post['NOMOR_INDUK'] = $this->input->post( 'NOMOR_INDUK' );
				$data_post['NM_PEGAWAI'] = $this->input->post( 'NM_PEGAWAI' );
				$data_post['INISIAL'] = $this->input->post( 'INISIAL' );
				$data_post['KOTA_LAHIR'] = $this->input->post( 'KOTA_LAHIR' );
				$data_post['TANGGAL_LAHIR'] = $this->input->post( 'TANGGAL_LAHIR' );
				$data_post['KD_JENIS_KELAMIN'] = $this->input->post( 'KD_JENIS_KELAMIN' );
				$data_post['KD_GOL_DARAH'] = $this->input->post( 'KD_GOL_DARAH' );
				$data_post['KD_AGAMA'] = $this->input->post( 'KD_AGAMA' );
				$data_post['KD_STATUS_NIKAH'] = $this->input->post( 'KD_STATUS_NIKAH' );
				$data_post['ALAMAT'] = $this->input->post( 'ALAMAT' );
				$data_post['KD_POS'] = $this->input->post( 'KD_POS' );
				$data_post['NO_TELP'] = $this->input->post( 'NO_TELP' );
				$data_post['KD_JENIS_KETENAGAAN'] = $this->input->post( 'KD_JENIS_KETENAGAAN' );
				$data_post['NO_HP'] = $this->input->post( 'NO_HP' );
				$data_post['DIR_FOTO'] = $this->input->post( 'DIR_FOTO' );
				$data_post['USERNAME'] = $this->input->post( 'USERNAME' );
				$data_post['TANGGAL_AKSES'] = $this->input->post( 'TANGGAL_AKSES' );
				$data_post['NOMOR_INDUK_BARU'] = $this->input->post( 'NOMOR_INDUK_BARU' );
				$data_post['NUPTK'] = $this->input->post( 'NUPTK' );
				$data_post['GELAR_DEPAN'] = $this->input->post( 'GELAR_DEPAN' );
				$data_post['GELAR_BELAKANG'] = $this->input->post( 'GELAR_BELAKANG' );
				$data_post['NIY_NIGK'] = $this->input->post( 'NIY_NIGK' );
				$data_post['NIK'] = $this->input->post( 'NIK' );
				$data_post['NM_IBU_KANDUNG'] = $this->input->post( 'NM_IBU_KANDUNG' );
				$data_post['RT'] = $this->input->post( 'RT' );
				$data_post['RW'] = $this->input->post( 'RW' );
				$data_post['KELURAHAN_DESA'] = $this->input->post( 'KELURAHAN_DESA' );
				$data_post['KECAMATAN'] = $this->input->post( 'KECAMATAN' );
				$data_post['KABUPATEN_KOTA'] = $this->input->post( 'KABUPATEN_KOTA' );
				$data_post['PROPINSI'] = $this->input->post( 'PROPINSI' );
				$data_post['KD_AREA'] = $this->input->post( 'KD_AREA' );
				$data_post['EMAIL'] = $this->input->post( 'EMAIL' );
				$data_post['STATUS_KEPEGAWAIAN'] = $this->input->post( 'STATUS_KEPEGAWAIAN' );
				$data_post['SERTIFIKASI_JABATAN'] = $this->input->post( 'SERTIFIKASI_JABATAN' );
				$data_post['TAHUN_SERTIFIKAT_JABATAN'] = $this->input->post( 'TAHUN_SERTIFIKAT_JABATAN' );
				$data_post['NOMOR_SERTIFIKAT_JABATAN'] = $this->input->post( 'NOMOR_SERTIFIKAT_JABATAN' );
				$data_post['KD_SERTIFIKASI_BIDANG_STUDI'] = $this->input->post( 'KD_SERTIFIKASI_BIDANG_STUDI' );
				$data_post['KD_PROGRAM_KEAHLIAN'] = $this->input->post( 'KD_PROGRAM_KEAHLIAN' );
				$data_post['LISENSI_KEPALA_SEKOLAH'] = $this->input->post( 'LISENSI_KEPALA_SEKOLAH' );
				$data_post['CATATAN'] = $this->input->post( 'CATATAN' );

                if ( $this->form_validation->run() == FALSE )
                {
                    $errors = validation_errors();
                    
                    
                    
                    
              		$this->template->assign( 'errors', $errors );
              		$this->template->assign( 'action_mode', 'create' );
            		$this->template->assign( 't_pegawai_data', $data_post );
            		$this->template->assign( 't_pegawai_fields', $fields );
                    $this->template->assign( 'metadata', $this->model_t_pegawai->metadata() );
            		$this->template->assign( 'table_name', 'T_pegawai' );
            		$this->template->assign( 'template', 'form_t_pegawai' );
            		$this->template->display( 'frame_admin.tpl' );
                }
                elseif ( $this->form_validation->run() == TRUE )
                {
                    $insert_id = $this->model_t_pegawai->insert( $data_post );
                    
					redirect( 't_pegawai' );
                }
            break;
        }
    }



    /**
     *  DISPLAYS THE POPULATED FORM OF THE RECORD
     *  This method uses the same template as the create method
     */
    function edit( $id = false )
    {
        $this->load->library('form_validation');

        switch ( $_SERVER ['REQUEST_METHOD'] )
        {
            case 'GET':
                $this->model_t_pegawai->raw_data = TRUE;
        		$data = $this->model_t_pegawai->get( $id );
                $fields = $this->model_t_pegawai->fields();
                
                
                
                
          		$this->template->assign( 'action_mode', 'edit' );
        		$this->template->assign( 't_pegawai_data', $data );
        		$this->template->assign( 't_pegawai_fields', $fields );
                $this->template->assign( 'metadata', $this->model_t_pegawai->metadata() );
        		$this->template->assign( 'table_name', 'T_pegawai' );
        		$this->template->assign( 'template', 'form_t_pegawai' );
        		$this->template->assign( 'record_id', $id );
        		$this->template->display( 'frame_admin.tpl' );
            break;
    
            case 'POST':
    
                $fields = $this->model_t_pegawai->fields();
                /* we set the rules */
                /* don't forget to edit these */
				$this->form_validation->set_rules( 'NOMOR_INDUK', lang('NOMOR_INDUK'), 'required|max_length[9]' );
				$this->form_validation->set_rules( 'NM_PEGAWAI', lang('NM_PEGAWAI'), 'required|max_length[50]' );
				$this->form_validation->set_rules( 'INISIAL', lang('INISIAL'), 'required|max_length[3]' );
				$this->form_validation->set_rules( 'KOTA_LAHIR', lang('KOTA_LAHIR'), 'required|max_length[50]' );
				$this->form_validation->set_rules( 'TANGGAL_LAHIR', lang('TANGGAL_LAHIR'), 'required' );
				$this->form_validation->set_rules( 'KD_JENIS_KELAMIN', lang('KD_JENIS_KELAMIN'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'KD_GOL_DARAH', lang('KD_GOL_DARAH'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'KD_AGAMA', lang('KD_AGAMA'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'KD_STATUS_NIKAH', lang('KD_STATUS_NIKAH'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'ALAMAT', lang('ALAMAT'), 'required|max_length[150]' );
				$this->form_validation->set_rules( 'KD_POS', lang('KD_POS'), '5' );
				$this->form_validation->set_rules( 'NO_TELP', lang('NO_TELP'), '30' );
				$this->form_validation->set_rules( 'KD_JENIS_KETENAGAAN', lang('KD_JENIS_KETENAGAAN'), 'required|max_length[1]' );
				$this->form_validation->set_rules( 'NO_HP', lang('NO_HP'), '30' );
				$this->form_validation->set_rules( 'DIR_FOTO', lang('DIR_FOTO'), '255' );
				$this->form_validation->set_rules( 'USERNAME', lang('USERNAME'), 'required|max_length[20]' );
				$this->form_validation->set_rules( 'TANGGAL_AKSES', lang('TANGGAL_AKSES'), 'required' );
				$this->form_validation->set_rules( 'NOMOR_INDUK_BARU', lang('NOMOR_INDUK_BARU'), '18' );
				$this->form_validation->set_rules( 'NUPTK', lang('NUPTK'), '16' );
				$this->form_validation->set_rules( 'GELAR_DEPAN', lang('GELAR_DEPAN'), '20' );
				$this->form_validation->set_rules( 'GELAR_BELAKANG', lang('GELAR_BELAKANG'), '20' );
				$this->form_validation->set_rules( 'NIY_NIGK', lang('NIY_NIGK'), '20' );
				$this->form_validation->set_rules( 'NIK', lang('NIK'), '20' );
				$this->form_validation->set_rules( 'NM_IBU_KANDUNG', lang('NM_IBU_KANDUNG'), '100' );
				$this->form_validation->set_rules( 'RT', lang('RT'), '3' );
				$this->form_validation->set_rules( 'RW', lang('RW'), '3' );
				$this->form_validation->set_rules( 'KELURAHAN_DESA', lang('KELURAHAN_DESA'), '100' );
				$this->form_validation->set_rules( 'KECAMATAN', lang('KECAMATAN'), '100' );
				$this->form_validation->set_rules( 'KABUPATEN_KOTA', lang('KABUPATEN_KOTA'), '100' );
				$this->form_validation->set_rules( 'PROPINSI', lang('PROPINSI'), '100' );
				$this->form_validation->set_rules( 'KD_AREA', lang('KD_AREA'), '5' );
				$this->form_validation->set_rules( 'EMAIL', lang('EMAIL'), '255' );
				$this->form_validation->set_rules( 'STATUS_KEPEGAWAIAN', lang('STATUS_KEPEGAWAIAN'), '1' );
				$this->form_validation->set_rules( 'SERTIFIKASI_JABATAN', lang('SERTIFIKASI_JABATAN'), '1' );
				$this->form_validation->set_rules( 'TAHUN_SERTIFIKAT_JABATAN', lang('TAHUN_SERTIFIKAT_JABATAN'), '4' );
				$this->form_validation->set_rules( 'NOMOR_SERTIFIKAT_JABATAN', lang('NOMOR_SERTIFIKAT_JABATAN'), '50' );
				$this->form_validation->set_rules( 'KD_SERTIFIKASI_BIDANG_STUDI', lang('KD_SERTIFIKASI_BIDANG_STUDI'), '3' );
				$this->form_validation->set_rules( 'KD_PROGRAM_KEAHLIAN', lang('KD_PROGRAM_KEAHLIAN'), '2' );
				$this->form_validation->set_rules( 'LISENSI_KEPALA_SEKOLAH', lang('LISENSI_KEPALA_SEKOLAH'), '1' );
				$this->form_validation->set_rules( 'CATATAN', lang('CATATAN'), '250' );

				$data_post['NOMOR_INDUK'] = $this->input->post( 'NOMOR_INDUK' );
				$data_post['NM_PEGAWAI'] = $this->input->post( 'NM_PEGAWAI' );
				$data_post['INISIAL'] = $this->input->post( 'INISIAL' );
				$data_post['KOTA_LAHIR'] = $this->input->post( 'KOTA_LAHIR' );
				$data_post['TANGGAL_LAHIR'] = $this->input->post( 'TANGGAL_LAHIR' );
				$data_post['KD_JENIS_KELAMIN'] = $this->input->post( 'KD_JENIS_KELAMIN' );
				$data_post['KD_GOL_DARAH'] = $this->input->post( 'KD_GOL_DARAH' );
				$data_post['KD_AGAMA'] = $this->input->post( 'KD_AGAMA' );
				$data_post['KD_STATUS_NIKAH'] = $this->input->post( 'KD_STATUS_NIKAH' );
				$data_post['ALAMAT'] = $this->input->post( 'ALAMAT' );
				$data_post['KD_POS'] = $this->input->post( 'KD_POS' );
				$data_post['NO_TELP'] = $this->input->post( 'NO_TELP' );
				$data_post['KD_JENIS_KETENAGAAN'] = $this->input->post( 'KD_JENIS_KETENAGAAN' );
				$data_post['NO_HP'] = $this->input->post( 'NO_HP' );
				$data_post['DIR_FOTO'] = $this->input->post( 'DIR_FOTO' );
				$data_post['USERNAME'] = $this->input->post( 'USERNAME' );
				$data_post['TANGGAL_AKSES'] = $this->input->post( 'TANGGAL_AKSES' );
				$data_post['NOMOR_INDUK_BARU'] = $this->input->post( 'NOMOR_INDUK_BARU' );
				$data_post['NUPTK'] = $this->input->post( 'NUPTK' );
				$data_post['GELAR_DEPAN'] = $this->input->post( 'GELAR_DEPAN' );
				$data_post['GELAR_BELAKANG'] = $this->input->post( 'GELAR_BELAKANG' );
				$data_post['NIY_NIGK'] = $this->input->post( 'NIY_NIGK' );
				$data_post['NIK'] = $this->input->post( 'NIK' );
				$data_post['NM_IBU_KANDUNG'] = $this->input->post( 'NM_IBU_KANDUNG' );
				$data_post['RT'] = $this->input->post( 'RT' );
				$data_post['RW'] = $this->input->post( 'RW' );
				$data_post['KELURAHAN_DESA'] = $this->input->post( 'KELURAHAN_DESA' );
				$data_post['KECAMATAN'] = $this->input->post( 'KECAMATAN' );
				$data_post['KABUPATEN_KOTA'] = $this->input->post( 'KABUPATEN_KOTA' );
				$data_post['PROPINSI'] = $this->input->post( 'PROPINSI' );
				$data_post['KD_AREA'] = $this->input->post( 'KD_AREA' );
				$data_post['EMAIL'] = $this->input->post( 'EMAIL' );
				$data_post['STATUS_KEPEGAWAIAN'] = $this->input->post( 'STATUS_KEPEGAWAIAN' );
				$data_post['SERTIFIKASI_JABATAN'] = $this->input->post( 'SERTIFIKASI_JABATAN' );
				$data_post['TAHUN_SERTIFIKAT_JABATAN'] = $this->input->post( 'TAHUN_SERTIFIKAT_JABATAN' );
				$data_post['NOMOR_SERTIFIKAT_JABATAN'] = $this->input->post( 'NOMOR_SERTIFIKAT_JABATAN' );
				$data_post['KD_SERTIFIKASI_BIDANG_STUDI'] = $this->input->post( 'KD_SERTIFIKASI_BIDANG_STUDI' );
				$data_post['KD_PROGRAM_KEAHLIAN'] = $this->input->post( 'KD_PROGRAM_KEAHLIAN' );
				$data_post['LISENSI_KEPALA_SEKOLAH'] = $this->input->post( 'LISENSI_KEPALA_SEKOLAH' );
				$data_post['CATATAN'] = $this->input->post( 'CATATAN' );

                if ( $this->form_validation->run() == FALSE )
                {
                    $errors = validation_errors();
                    
                    
                    
                    
              		$this->template->assign( 'action_mode', 'edit' );
              		$this->template->assign( 'errors', $errors );
            		$this->template->assign( 't_pegawai_data', $data_post );
            		$this->template->assign( 't_pegawai_fields', $fields );
                    $this->template->assign( 'metadata', $this->model_t_pegawai->metadata() );
            		$this->template->assign( 'table_name', 'T_pegawai' );
            		$this->template->assign( 'template', 'form_t_pegawai' );
        		    $this->template->assign( 'record_id', $id );
            		$this->template->display( 'frame_admin.tpl' );
                }
                elseif ( $this->form_validation->run() == TRUE )
                {
				    $this->model_t_pegawai->update( $id, $data_post );
				    
					redirect( 't_pegawai/show/' . $id );   
                }
            break;
        }
    }



    /**
     *  DELETE RECORD(S)
     *  The 'delete' method of the model accepts int and array  
     */
    function delete( $id = FALSE )
    {
        switch ( $_SERVER ['REQUEST_METHOD'] )
        {
            case 'GET':
                $this->model_t_pegawai->delete( $id );
                redirect( $_SERVER['HTTP_REFERER'] );
            break;

            case 'POST':
                $this->model_t_pegawai->delete( $this->input->post('delete_ids') );
                redirect( $_SERVER['HTTP_REFERER'] );
            break;
        }
    }
}
